package websocket;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

public class WssServerInitializer extends ChannelInitializer<SocketChannel>{

	@Override
	protected void initChannel(SocketChannel ch) throws Exception {
		// TODO Auto-generated method stub
		ChannelPipeline channelPipeline = ch.pipeline();
		//websocket 基于http协议
		channelPipeline.addLast(new HttpServerCodec());
		//对写大数据流的支持
		channelPipeline.addLast(new ChunkedWriteHandler());
		//对httpMessage进行聚合 聚合成FullHttpRequest或则FullHttpResponse
		//几乎所有netty编程都会用到
		channelPipeline.addLast(new HttpObjectAggregator(1024*64));
		
		//		====== 以上用于HTTP协议支持========
		
		//websocket 服务器处理的协议 用于制定给客户端访问的路由 /ws
		/**
		 * 帮你处理握手动作 （close ping pong） ping+pong=心跳
		 * 对于websocket来讲 都是以frames进行传输 ，不同的数据类型对应的frame不同
		 */
		channelPipeline.addLast(new WebSocketServerProtocolHandler("/myws"));
		//自定义的handler
		channelPipeline.addLast(new ChatHandler());
		
	}
	
}
